package com.southminority.ethnic.controller.admin.excel;

import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.pojo.Member;
import com.southminority.ethnic.service.member.MemberService;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.southminority.ethnic.until.ExcelUntil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@Tag(name = "excel-成员", description = "团队成员导入导出")
@RestController
@RequestMapping("/admin/member-excel")
@CrossOrigin
public class MemberExcelController {
    @Autowired
    private com.southminority.ethnic.mapper.member.MemberMapper memberMapper;

    @Autowired
    private MemberService memberService;

    @GetMapping("/output")
    @Operation(summary = "导出所有成员", description = "导出所有成员")
    public ResponseEntity<byte[]> outputExcel() {
        try {
            List<Member> dataList = memberMapper.selectList(new QueryWrapper<>());
            byte[] excelBytes = ExcelUntil.exportExcel(dataList);
            String fileName = "member_output";
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            String encodedFileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + ".xlsx";
            headers.setContentDispositionFormData("attachment", encodedFileName);
            return new ResponseEntity<>(excelBytes, headers, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping("/mb-input")
    @Operation(summary = "获取成员导入模版", description = "获取成员导入模版")
    public ResponseEntity<byte[]> mbInputExcel() {
        try {
            String fileName = "member_input";
            byte[] excelBytes = ExcelUntil.mbExcel(Member.class);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            String encodedFileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + ".xlsx";
            headers.setContentDispositionFormData("attachment", encodedFileName);
            return new ResponseEntity<>(excelBytes, headers, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/input")
    @Operation(summary = "导入成员", description = "导入成员")
    public R<String> importAndSave(@RequestParam("file") MultipartFile file) {
        try {
            List<Member> list = ExcelUntil.importExcel(file, Member.class);
            // 使用基础 Mapper 批量保存/更新
            for (Member m : list) {
                if (m.getId() == null) {
                    memberMapper.insert(m);
                } else {
                    memberMapper.updateById(m);
                }
            }
            return R.ok("导入成功，共" + list.size() + "条数据");
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("导入失败：" + e.getMessage());
        }
    }
}


